const db = wx.cloud.database();
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
     display: "block",
        page: 38,  
        touch: {
            distance: 0,
            scale: 1,
            baseWidth: '',
            baseHeight: '',
            scaleWidth: 350,
            scaleHeight: 300
        }
       
    },
    touchstartCallback: function(e) {
        // 单手指缩放开始，也不做任何处理 
        if(e.touches.length == 1) return console.log('双手指触发开始')
        // 注意touchstartCallback 真正代码的开始 // 一开始我并没有这个回调函数，会出现缩小的时候有瞬间被放大过程的bug // 当两根手指放上去的时候，就将distance 初始化。 
        let xMove = e.touches[1].clientX - e.touches[0].clientX;
        let yMove = e.touches[1].clientY - e.touches[0].clientY;
        let distance = Math.sqrt(xMove * xMove + yMove * yMove);
        this.setData({
           'touch.distance': distance,
        })
    },
    touchmoveCallback: function(e) {
        let touch = this.data.touch
        // 单手指缩放我们不做任何操作 
        if(e.touches.length == 1) 
        return console.log('双手指运动')
        let xMove = e.touches[1].clientX - e.touches[0].clientX;
        let yMove = e.touches[1].clientY - e.touches[0].clientY;
        // 新的 ditance 
        let distance = Math.sqrt(xMove * xMove + yMove * yMove);
        let distanceDiff = distance - touch.distance;
        let newScale = touch.scale + 0.005 * distanceDiff
        // 为了防止缩放得太大，所以scale需要限制，同理最小值也是
         if(newScale >= 2) {
            newScale = 2
        }
        if(newScale <= 0.6) {
            newScale = 0.6
        }
        let scaleWidth = newScale * touch.baseWidth
        let scaleHeight = newScale * touch.baseHeight
        // 赋值 新的 => 旧的 
        this.setData({
           'touch.distance': distance,
           'touch.scale': newScale,
           'touch.scaleWidth': scaleWidth,
           'touch.scaleHeight': scaleHeight,
           'touch.diff': distanceDiff
        })
    },
    bindload: function(e) {
      // bindload 这个api是<image>组件的api类似<img>的onload属性 
      this.setData({
          'touch.baseWidth': e.detail.width,
          'touch.baseHeight': e.detail.height,
          'touch.scaleWidth': e.detail.width,
          'touch.scaleHeight': e.detail.height
      })
    },


    onLoad: function (options) {

        var that = this;
        console.log(options)
        that.setData({
            area: options.area,
            geneal: options.agent
        })
        if (options.page) {
            that.setData({
                page: options.page
            })
        }
        
    },

    getPhoneNumber(e) {
        var that = this;
            wx.cloud.callFunction({
                name: 'getMobile',
                data: {
                    weRunData: wx.cloud.CloudID(e.detail.cloudID),
                }
            }).then(res => {
                that.setData({
                    mobile: res.result,
                })
                wx.setStorageSync('phone', res.result)
                console.log(res.result)
                wx.request({ //发送请求
                    url: "https://zg99.offcn.com/index/biaodan/register/?actid=35888&callback=?",
                    data: {
                        name: that.data.inputValue,
                        xueli: that.data.xueLi,
                        yuanxiao:that.data.inputValueT,
                        phone: res.result,
                        fenxiao: that.data.area,
                        geneal: that.data.geneal
                    }, //接收参数
                    success(res) { //返回
                        var jsonStr = res.data;
                        jsonStr = jsonStr.replace("(", "");
                        jsonStr = jsonStr.replace(")", "");
                        if (typeof jsonStr != 'object') {
                            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                            var theJson = JSON.parse(jsonStr);
                        };
                        console.log(theJson)
                        that.setData({
                            display: "none"
                          })          
    
                    }
                })
    
                wx.request({
                    url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/wx_collect_info',
                    method: 'POST',
                    data: {
                        activity_id: 30,
                        mobile: res.result,
                        area_code: that.data.area,
                        agent: that.data.geneal,
                        extra: {
                            dishi: that.data.dishi,
                            zkz: that.data.zkz,
                        },
                        page_index: that.data.page
                    },
                    success: res => {
                        console.log(res);
                    }
                })
    
            }).catch(err => {
                console.error(err);
            });
    },
    info(e) {
        wx.request({ //发送请求
            url: "https://zg99.offcn.com/index/chaxun/getlist/?actid=35888&callback=?",
            data: {
                province: '山东省',
                city:'',
                limits:'500'
              
            }, //接收参数
            success(res) { //返回
                var jsonStr = res.data;
                jsonStr = jsonStr.replace("(", "");
                jsonStr = jsonStr.replace(")", "");
                if (typeof jsonStr != 'object') {
                    jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                    var theJson = JSON.parse(jsonStr);
                };
                console.log(theJson)

            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})